package com.gomoney.auth.controller;

import com.gomoney.auth.service.SysMenuService;
import com.gomoney.auth.service.SysMenuService;
import com.gomoney.oauth2.common.entity.sys.SysMenu;
import com.gomoney.oauth2.common.exception.RRException;
import com.gomoney.oauth2.common.util.Constant;
import com.gomoney.oauth2.common.util.R;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends AbstractController {

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation("登陆后获取登录用户菜单信息")
    @GetMapping("/nav")
    public R nav() {
        return sysMenuService.nav(getUser());
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public List<SysMenu> list() {
        return sysMenuService.findMenuForAll(1);
    }

    @GetMapping("/select")
    @PreAuthorize("hasAuthority('sys:menu:select')")
    public R select() {
        //查询列表数据
        List<SysMenu> menuList = sysMenuService.findMenuForAll(1);
        //添加顶级菜单
        SysMenu root = new SysMenu();
        root.setMenuId("1");
        root.setMenuName("一级菜单");
        root.setParentMenuId(null);
        root.setOpen(true);
        menuList.add(root);
        return R.ok().put("menuList", menuList);
    }

    @PostMapping("/save")
    @PreAuthorize("hasAuthority('sys:menu:save')")
    public R save(@RequestBody SysMenu menu) {
        //数据校验
        verifyForm(menu);
        sysMenuService.addMenu(menu);
        return R.ok();
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('sys:menu:update')")
    public R update(@RequestBody SysMenu menu) {
        //数据校验
        verifyForm(menu);
        sysMenuService.updateMenu(menu);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    public R delete(@RequestParam Map<String, Object> params) {
/*        if(params.get("menuId").equals("MOO1")){
            return R.error("系统菜单，不能删除");
        }*/
        //判断是否有子菜单或按钮
        List<SysMenu> menuList = sysMenuService.findChildMenuById(params.get("menuId").toString());
        if (menuList.size() > 0) {
            return R.error("请先删除子菜单或按钮");
        }
        sysMenuService.delMenu(params.get("menuId").toString());
        return R.ok();
    }

    @ApiOperation("查看菜单信息")
    @GetMapping("/info")
    @PreAuthorize("hasAuthority('sys:menu:info')")
    public R info(@RequestParam Map<String, Object> params) {
        SysMenu menu = sysMenuService.findMenuById(params.get("menuId").toString());
        return R.ok().put("menu", menu);
    }

    /**
     * 验证参数是否正确
     */
    private void verifyForm(SysMenu menu) {
        if (StringUtils.isBlank(menu.getMenuName())) {
            throw new RRException("菜单名称不能为空");
        }

        if (menu.getParentMenuId() == null) {
            throw new RRException("上级菜单不能为空");
        }

        //菜单
        if (menu.getType() == Constant.MenuType.MENU.getValue()) {
            if (StringUtils.isBlank(menu.getUrl())) {
                throw new RRException("菜单URL不能为空");
            }
        }

        //上级菜单类型
        int parentType = Constant.MenuType.CATALOG.getValue();
        if (!menu.getParentMenuId().equals("1")) {
            SysMenu parentMenu = sysMenuService.findMenuById(menu.getParentMenuId());
            parentType = parentMenu.getType();
        } else {
            menu.setParentMenuId(null);
        }

        //目录、菜单
        if (menu.getType() == Constant.MenuType.CATALOG.getValue() ||
                menu.getType() == Constant.MenuType.MENU.getValue()) {
            if (parentType != Constant.MenuType.CATALOG.getValue()) {
                throw new RRException("上级菜单只能为目录类型");
            }
            return;
        }

        //按钮
        if (menu.getType() == Constant.MenuType.BUTTON.getValue()) {
            if (parentType != Constant.MenuType.MENU.getValue()) {
                throw new RRException("上级菜单只能为菜单类型");
            }
            return;
        }
    }
}
